﻿using FtpFileBackup_Oracle.LogHelper;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FtpFileBackup_Oracle.DbHelper
{
    public class SqlServerDb
    {
        SqlConnection conn = null;
        SqlCommand cmd = null;
        public SqlServerDb()
        {
            conn = new SqlConnection();
            conn.ConnectionString = ConfigurationManager.ConnectionStrings["Default"].ConnectionString;
            cmd = new SqlCommand();
            cmd.Connection = conn;
            cmd.CommandTimeout = 0;

        }
        public SqlServerDb(string constr)
        {
            conn = new SqlConnection();
            conn.ConnectionString = constr;
            cmd = new SqlCommand();
            cmd.Connection = conn;
            cmd.CommandTimeout = 0;
        }

        /// <summary>
        /// 执行sql
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        public bool ExecuteNonQuery(string sql)
        {
            bool succ = false;
            try
            {
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = sql;
                OpenConn();
                succ = cmd.ExecuteNonQuery() > 0;
                CloseConn();
            }
            catch (SqlException sexc)
            {
                log4netHelper.Error(sexc.Message);
                return false;
            }
            catch (Exception ex)
            {
                log4netHelper.Error(ex.Message);
                return false;
            }
            return succ;
        }

        /// <summary>
        /// 打开连接
        /// </summary>
        public void OpenConn()
        {
            if (conn.State != ConnectionState.Open)
            {
                try
                {
                    conn.Open();
                }
                catch (Exception ex)
                {
                    log4netHelper.Error(ex.Message);
                    return;
                }
                log4netHelper.Info($"打开数据库连接");
            }
        }
        /// <summary>
        /// 关闭连接
        /// </summary>
        public void CloseConn()
        {
            if (conn.State != ConnectionState.Closed)
            {
                try
                {
                    conn.Close();
                }
                catch (Exception ex)
                {
                    log4netHelper.Error(ex.Message);
                    return;
                }
                log4netHelper.Info($"关闭数据库连接");
            }
        }
        public void DisposeConn()
        {
            try
            {
                if (conn.State != ConnectionState.Closed)
                {
                    try
                    {
                        conn.Close();

                    }
                    catch (Exception ex)
                    {
                        log4netHelper.Error(ex.Message);
                        return;
                    }
                }
            }
            catch
            {

            }
        }
    }
}
